package cn.bdqn.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 *  编写一个过滤器，该过滤器拦截所有的请求（value="/*"）
 *  过滤器做编码处理
 **/

@WebFilter(filterName = "EncodeFilter", value = "/*")
public class EncodeFilter implements Filter {

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        //统一处理请求和响应编码
        //设置请求参数的编码格式，这种方式对get请求方式无效
        request.setCharacterEncoding("UTF-8");
        //设置响应编码格式为UTF-8
        response.setContentType("text/html;charset=UTF-8");

        //放行
        chain.doFilter(request, response);

    }

}